package com.qst.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.*;
import com.qst.service.HouselistService;
import com.qst.service.UserlistService;
import com.qst.service.ZulistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller @RequestMapping("/zulist") public class ZulistController {
	 @Autowired private ZulistService zulistService;
	 @Autowired private UserlistService userlistService;
	 @Autowired private HouselistService houselistService;

	 // 跳到增添合同的页面
	 @RequestMapping("/toaddhetong") public String toaddhetong(Model model, String house_id,
			 Integer zkid, Integer fdid) {
		  Userlist zlist = userlistService.getUserlist(zkid);//租客信息
		  Userlist fdlist = userlistService.findhasuserlist(fdid);//房东信息
		  Houselist houselist = houselistService.findhouseid(house_id);

		  Hetong hetong = new Hetong();
		  hetong.setHouse_id(house_id);
		  if (fdlist != null) {
				hetong.setChuzu(fdlist.getName());
				hetong.setChuzu_idcard(fdlist.getIdcard());
		  }
		  if (zlist != null) {
				hetong.setZuke(zlist.getName());
				hetong.setZuke_idcard(zlist.getIdcard());
		  }
		  hetong.setAddress(houselist.getAddress());
		  hetong.setPrice(houselist.getPrice());
		  model.addAttribute("hetong", hetong);
		  model.addAttribute("mainPage", "../hetong/addhetong.jsp");
		  return "common/main";
	 }

	 // 管理员查看所有在租列表,房东查看自己的
	 @RequestMapping("/findzulist") public String findzulist(Model model,
			 @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
			 HttpSession httpSession) throws Exception {
		  PageHelper.startPage(page, pageSize);
		  User user = (User) httpSession.getAttribute("user");
		  List<Zulist> zulist = new ArrayList<Zulist>();
		  if ("admin".equals(user.getUsertype())) {
				zulist = zulistService.findAllZuList();
				System.out.println("admin~~~");
		  } else {
				zulist = zulistService.findZuList(user.getId());
				System.out.println("fangdong~~~");
		  }
		  PageInfo<Zulist> p = new PageInfo<Zulist>(zulist);
		  model.addAttribute("p", p);
		  model.addAttribute("zulist", zulist);
		  model.addAttribute("mainPage", "../common/zulist.jsp");
		  return "common/main";
	 }

	 // 查看我的在租列表
	 @RequestMapping("/myzulist") public String myzulist(Model model, HttpSession httpSession,
			 @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize) throws Exception {

		  User user1 = (User) httpSession.getAttribute("user");
		  Userlist userlist = userlistService.findhasuserlist(user1.getId());
		  PageHelper.startPage(page, pageSize);
		  List<Userlist> list = userlistService.getUserzuList(userlist.getId());
		  PageInfo<Userlist> p = new PageInfo<Userlist>(list);
		  model.addAttribute("userlistzu", list);
		  model.addAttribute("p", p);
		  model.addAttribute("mainPage", "../zulist/myzulist.jsp");

		  return "common/main";
	 }

}
